package org.jeecg.modules.sistersophie.facade.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.sistersophie.config.AccountTypeEnums;
import org.jeecg.modules.sistersophie.entity.AccountInfo;
import org.jeecg.modules.sistersophie.entity.CourseInfo;
import org.jeecg.modules.sistersophie.entity.StudentInfo;
import org.jeecg.modules.sistersophie.facade.CourseStudentFacade;
import org.jeecg.modules.sistersophie.facade.StudentFacade;
import org.jeecg.modules.sistersophie.service.IAccountInfoService;
import org.jeecg.modules.sistersophie.service.IStudentInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class StudentFacadeService implements StudentFacade {

    @Autowired
    private IStudentInfoService studentInfoService;

    @Autowired
    private IAccountInfoService accountInfoService;

    @Autowired
    private CourseStudentFacade courseStudentFacade;

    public List<StudentInfo> selectStudentList(Wrapper<StudentInfo> queryWrapper){
        return studentInfoService.selectStudentList(queryWrapper);
    }

    @Transactional
    public boolean modifyStudent(StudentInfo studentInfo){
        return studentInfoService.updateById(studentInfo);
    }

    public StudentInfo selectById(Integer id){
        QueryWrapper<StudentInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id).eq("is_delete",0);
        List<StudentInfo> resList = studentInfoService.list(queryWrapper);
        if(CollectionUtils.isNotEmpty(resList)){
            return resList.get(0);
        }
        return null;
    }

    @Override
    @Transactional
    public boolean save(StudentInfo studentInfo) {
        StudentInfo qInfo = new StudentInfo();
        //qInfo.setFirstName(studentInfo.getFirstName());
        //qInfo.setLastName(studentInfo.getLastName());
        qInfo.setChineseName(studentInfo.getChineseName());
        qInfo.setIsDelete(0);

        if((StringUtils.isNotBlank(studentInfo.getPhone()) && StringUtils.isBlank(studentInfo.getPhonePrefix())) ||
                (StringUtils.isBlank(studentInfo.getPhone()) && StringUtils.isNotBlank(studentInfo.getPhonePrefix())))
        {
            throw new RuntimeException(" Please enter complete phone information");
        }

        if(StringUtils.isNotBlank(studentInfo.getPhone()) && StringUtils.isNotBlank(studentInfo.getPhonePrefix())){
            studentInfo.setPhone(studentInfo.getPhonePrefix()+"-" + studentInfo.getPhone());
        }
        //判断学生名称是否重复
        QueryWrapper<StudentInfo> queryWrapper = QueryGenerator.initQueryWrapper(qInfo, new HashMap<>());
        List<StudentInfo> studentInfoList =  studentInfoService.list(queryWrapper);
        if(studentInfoList != null && studentInfoList.size() > 0){
            throw new RuntimeException(studentInfo.getChineseName()  + " already exists, please re-enter");
        }

        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        studentInfo.setCreateName(sysUser.getUsername());
        studentInfo.setCreateTime(new Date());
        studentInfo.setUpdateName(sysUser.getUsername());
        studentInfo.setUpdateTime(new Date());
        studentInfo.setIsDelete(0);

        boolean bl = studentInfoService.save(studentInfo);
        //创建学生账户信息
        if(!bl){
            throw new RuntimeException("Save failed");
        }
        AccountInfo accountInfo = new AccountInfo();
        accountInfo.setStudentId(studentInfo.getId());
        accountInfo.setAccountType(AccountTypeEnums.TYPE_1.getType());
        accountInfo.setAmount(BigDecimal.ZERO);
        accountInfo.setFreeAmount(BigDecimal.ZERO);
        accountInfo.setAvailableAmount(BigDecimal.ZERO);
        accountInfo.setCreateName(sysUser.getUsername());
        accountInfo.setCreateTime(new Date());
        accountInfo.setUpdateName(sysUser.getUsername());
        accountInfo.setUpdateTime(new Date());
        accountInfo.setIsDelete(0);
        bl = accountInfoService.save(accountInfo);
        return bl;
    }

    public boolean edit(StudentInfo studentInfo){
        if((StringUtils.isNotBlank(studentInfo.getPhone()) && StringUtils.isBlank(studentInfo.getPhonePrefix())) ||
                (StringUtils.isBlank(studentInfo.getPhone()) && StringUtils.isNotBlank(studentInfo.getPhonePrefix())))
        {
            throw new RuntimeException(" Please enter complete phone information");
        }
        if(StringUtils.isNotBlank(studentInfo.getPhone()) && StringUtils.isNotBlank(studentInfo.getPhonePrefix())){
            studentInfo.setPhone(studentInfo.getPhonePrefix()+"-" + studentInfo.getPhone());
        }
        StudentInfo qInfo = new StudentInfo();
        qInfo.setId(studentInfo.getId());
        qInfo.setIsDelete(0);
        QueryWrapper<StudentInfo> queryWrapper = QueryGenerator.initQueryWrapper(qInfo, new HashMap<>());
        StudentInfo stInfo =  studentInfoService.getOne(queryWrapper);
        if(stInfo == null){
            throw new RuntimeException(studentInfo.getFirstName() + " " + studentInfo.getLastName() + " information does not exist");
        }
        List<StudentInfo> check = studentInfoService.selectByChineseNameNotIn(studentInfo.getChineseName(),studentInfo.getId());
        if(CollectionUtils.isNotEmpty(check)){
            throw new RuntimeException(studentInfo.getChineseName() + " already exists, please re-enter");
        }

       return  studentInfoService.saveOrUpdate(studentInfo);
    }

    public void deleteStudent(String id){
        //如果当前课程 存在关联的 学生 不允许删除
        StudentInfo studentInfo = studentInfoService.getById(id);
        int num = courseStudentFacade.selectCourseCountByStudent(studentInfo.getId());
        if(num > 0){
            throw new RuntimeException(" The student is already associated with course information, this operation is not allowed ");
        }
        studentInfo.setIsDelete(1);
        studentInfoService.updateById(studentInfo);
    }
}
